package cn.chint.manage.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.chint.bo.UserPages;
import cn.chint.bo.Userbo;
import cn.chint.manage.dao.UserDao;
import cn.chint.manage.service.UserService;
import cn.chint.po.User;
import cn.chint.util.HTTPClientUtils;
import cn.chint.util.UtilDate;

@Controller
public class ShareController { 
	@Autowired
	UserService userService;
	@Autowired
	UserDao userDao;

	public Logger log = Logger.getLogger(getClass());

	@RequestMapping(value = "share", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String  share(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response)  {

		    String url = request.getParameter("url");
		    com.alibaba.fastjson.JSONObject o = new  com.alibaba.fastjson.JSONObject();
		    try{
		    String token =  this.getaccess_token();
		    log.info("token="+token);
		    
	        String jsapi_ticket =this.getTicket(token);;//jsapi_ticket
	        log.info("jsapi_ticket="+jsapi_ticket);
	        
	        String timestamp = Long.toString(System.currentTimeMillis() / 1000);//时间戳
	        log.info("timestamp="+timestamp);
	        
	        String nonceStr = UUID.randomUUID().toString();
	        log.info("nonceStr="+nonceStr);
	        
	        String signature = cn.chint.util.SignUtil.getSignature( jsapi_ticket, nonceStr, timestamp,  url);//验证签名
	        log.info("signature="+signature);
	        
	        
	        o.put("url", url);
	        o.put("jsapi_ticket", jsapi_ticket);
	        o.put("nonceStr", nonceStr);
	        o.put("timestamp", timestamp);
	        o.put("signature", signature);
	        String appid="wx487b598049f3a162"   ;
	        o.put("appid",appid);
	        o.put("status","1");
	        
	        return o.toJSONString();
		    }catch(Exception e){
		    	e.printStackTrace();
		    	o.put("status","0");
		    	return o.toJSONString();
		    }
		
	}
	
	public  String getaccess_token() throws Exception{
		String appid="wx487b598049f3a162"   ;
		String secret = "7afd7b083512837aad16295d2de49b22";
		/**获取TOKEN信息*/
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
		log.info("token请求地址: "+ url);
		String res_json = HTTPClientUtils.getRequest(url);
		log.info("返回token信息: "+ res_json);
		
		com.alibaba.fastjson.JSONObject o = com.alibaba.fastjson.JSONObject.parseObject(res_json);
		String token = o.getString("access_token");
		return token;

	}
	
	   //获取ticket
    @SuppressWarnings("unused")
	private  String getTicket(String access_token) throws Exception {  
        String ticket = null;  
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+ access_token +"&type=jsapi";//这个url链接和参数不能变  
       
        String res_json = HTTPClientUtils.getRequest(url);
    	log.info("返回token信息: "+ res_json);
    		
    	com.alibaba.fastjson.JSONObject o = com.alibaba.fastjson.JSONObject.parseObject(res_json);
        ticket = o.getString("ticket");  
            
       
        return ticket;  
    } 
    
	
    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "focus", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
   	public Map focus(HttpServletRequest request) throws Exception{
    	String page = request.getParameter("page");
		String size = request.getParameter("size");
		String wx_nickname = request.getParameter("wx_nickname");
		UserPages userPages;
		if("".equals(wx_nickname) || wx_nickname == null){
			userPages =  userDao.Focus_user(Integer.parseInt(page),Integer.parseInt(size));
		}else{
			userPages =  userDao.focus(Integer.parseInt(page),Integer.parseInt(size), wx_nickname);
		}
		List<Userbo> list = userPages.getList();
       	Map<String, Object> r = new HashMap<String, Object>();
       	r.put("code", 0);
		r.put("msg", "");
		r.put("count", userPages.getUser_num());
		r.put("data", list);
   		return r;
       }
	
}





